package com.wondersgroup.fjzlpt.yyzyzx.service.impl.yyzycx;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.util.ExcelOrWordUtils;
import com.wondersgroup.fjzlpt.yyzyzx.dao.inter.yyzycx.YjxxDAO;
import com.wondersgroup.fjzlpt.yyzyzx.service.inter.yyzycx.YjxxService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 医技信息查询实现层
 * <p>
 * <p>
 * Created by sanma on 2017/8/7.
 */
@Service
public class YjxxServiceImpl implements YjxxService {


    @Autowired
    private YjxxDAO yjxxDao;

    /**
     * 分页查询医技信息
     * @param map
     * @return
     */
    @Override
    public JSONObject pageQueryYjxxAll(Map map) {
        return yjxxDao.pageQueryYjxxAll(map);
    }

    /**
     * 查询所有数据
     * @param map
     * @return
     */
	@Override
	public void queryAllForExcel(Map map, HttpServletRequest request, HttpServletResponse response) {
		JSONArray downData = yjxxDao.queryAll(map);
		for(int i=0;i<downData.size();i++){
			JSONObject item = downData.getJSONObject(i);
			Object yyrq = item.get("YYRQ");
			if(yyrq != null){
				item.put("YYRQ", yyrq.toString().substring(0, 10));
			}
		}
		
		//表头
		StringBuffer titleStr = new StringBuffer();
		titleStr.append("{'YLJGMC':'医院名称','KSMC':'科室名称','XMFLMC':'项目分类','XMMC':'项目名称','TWMC':'体位','YYRQ':'预约日期',")
				.append("'YYSJD':'时间段','SFJJ':'是否加检','SYKYYS':'剩余可预约数','YYYS':'已预约数'}");
		JSONObject title = JSONObject.parseObject(titleStr.toString());
		downData.add(0, title);
		
		//排列循序
		List<String> titelField = new ArrayList<>();
		titelField.add("YLJGMC");
		titelField.add("KSMC");
		titelField.add("XMFLMC");
		titelField.add("XMMC");
		titelField.add("TWMC");
		titelField.add("YYRQ");
		titelField.add("YYSJD");
		titelField.add("SFJJ");
		titelField.add("SYKYYS");
		titelField.add("YYYS");
		ExcelOrWordUtils.downLoadExcelFile(downData, titelField ,"实时床位列表",request, response);
		
	}
}
